package com.sunlands.zlcx.usercenter.controller;

import com.sunlands.zlcx.usercenter.common.annotation.AuthSig;
import com.sunlands.zlcx.usercenter.common.annotation.AutoLog;
import com.sunlands.zlcx.usercenter.domain.UserCardsDO;
import com.sunlands.zlcx.usercenter.exception.GlobalExceptionHandler;
import com.sunlands.zlcx.usercenter.service.UserCardsServiceImpl;
import com.sunlands.zlcx.usercenter.util.BeanPropertiesUtil;
import com.sunlands.zlcx.usercenter.vo.UserCardsVO;
import com.sunlands.zlcx.usercenter.vo.response.BusinessResult;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiResponse;
import io.swagger.annotations.ApiResponses;
import lombok.extern.slf4j.Slf4j;
import org.apache.http.util.Asserts;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @author anjl
 * @date 1:35 PM 2019/4/15
 * description:
 */
@SuppressWarnings("unused")
@Api(tags = "20、用户名片")
@ApiResponses(value = {@ApiResponse(code = 400, response = GlobalExceptionHandler.class, message = "数据校验失败"), @ApiResponse(code = 500, response = GlobalExceptionHandler.class, message = "内部错误")})
@Slf4j
@Configuration
@RestController
@RequestMapping("/user/cards")
public class UserCardsController implements AuthSupport, BeanPropertiesUtil {

    @AutoLog
    @ApiOperation(value = "用户名片上报", notes = "手机注册用户")
    @PostMapping
    @AuthSig
    public BusinessResult<UserCardsVO> addUserCards(@RequestBody UserCardsVO userCards) {
        Asserts.notEmpty(userCards.getCard(), "名片不能不为空！");
        Asserts.check(userCards.getUserId() >= 0, "用户id不合法");
        Asserts.notNull(userCards.getType(), "名片类型不能为空");

        UserCardsDO cardsDO = UserCardsVO.from(userCards);
        return BusinessResult.createSuccessInstance(userCardsService.save(cardsDO));
    }

    @Autowired
    private UserCardsServiceImpl userCardsService;

}
